package cn.ad.server.job.adapter.persistence.job;

import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import cn.ad.server.job.adapter.dbaccess.DbDataContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import cn.ad.server.job.adapter.dbaccess.AssembledCachedDataContainer;
import cn.ad.server.domain.SysConfig;
import cn.ad.server.job.domain.job.SysConfigWarmUpService;

@Repository
public class SysConfigWarmUpServant implements SysConfigWarmUpService {
	private static Logger logger = LoggerFactory.getLogger(SysConfigWarmUpServant.class);

	@Resource(name = "cacheRedisTemplate")
	private HashOperations<String, String, String> configHashOperations;

	@Resource(name = "cacheRedisTemplate")
	private RedisTemplate<String, String> redisTemplate;

	@Autowired
	private AssembledCachedDataContainer container;

	public void warmUpSysConfig() {

		Map<String, String> config = DbDataContainer.getSysConfigValues();
		Set<String> keySet = config.keySet();
		for (String key : keySet) {
			upToRedis(key, config.get(key), true);
		}
	}

	@Value("${cached.redisDebug:false}")
	private boolean redisDebug;

	void upToRedis(String key, String value, boolean operatorFlg) {
		if (!redisDebug) {
			return;
		}
		if (operatorFlg) {
			configHashOperations.put(SysConfig.CACHE_NAME + "_debug", key, value);
		} else {
			configHashOperations.delete(SysConfig.CACHE_NAME + "_debug", key);
		}
	}

}
